<?php
/**
 * PHP code block
 */
class My_Code_Block
{

    const BLOCK_SEPARATOR = "\n";

    /**
     * PHP code
     * @var string
     */
    protected $code;

    /**
     * @see Factory
     */
    protected function  __construct($code)
    {
        $this->code = $code;
    }

    /**
     * @param string $code
     * @return My_Code_Line
     */
    public static function factory($code)
    {
        return new self($code);
    }


    /**
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param string $code
     * @return My_Code_Line
     */
    public function setCode($code)
    {
        $this->code = $code;
        return $this;
    }

    /**
     * @return string
     */
    public function toString()
    {
        // Format code
        $search = array("  ", "\n", "\t", ";", "{", "}");
        $replace = array("", "", "", ";\n", "\n{\n", "}\n");
        $this->code = str_replace($search, $replace, $this->code);
        return $this->code.My_Code_Block::BLOCK_SEPARATOR;
    }
}
